<template>
  <el-row>
    <el-col :span="24" class="bg1">
      <div class="grid-content bg-purple">
        <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="110" :isSubmitBtn="false" @formChange="changeHandle" />
      </div>
      <div class="btn">
        <el-button size="small" icon="el-icon-search" type="primary" @click="query">查询</el-button>
        <el-button size="small" icon="el-icon-refresh-left" type="primary" @click="reset">重置</el-button>
      </div>
    </el-col>
    <el-col :span="24" class="bg2">
      <FilterTable
        ref="table"
        selectionType="single"
        :columns="columns"
        :fetchapi="fetchApi"
        :datakey="'rows'"
        :params="fetchParams"
        columnsRef="yxqPjxmTable"
        :isColumnFilter="false"
        :onColumnsChange="columns => (this.columns = columns)"
        :onRowSelectChange="arr => (this.onChangeColumn = arr)"
      >
      </FilterTable>
    </el-col>
    <el-col :span="24" style="margin-top:10px;">
      <div class="btn">
        <el-button size="small" icon="el-icon-folder-add" type="primary" class="btnStyle" @click="determine">确定</el-button>
        <el-button size="small" icon="el-icon-refresh-left" type="primary" class="btnStyle" @click="closeDialog">取消</el-button>
      </div>
    </el-col>
    <BaseDialog :visible.sync="visibleCxz" title="备件车系" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '10px' }" destroyOnClose>
      <newAddpjxm ref="newAddpjxm" @closePjxm="closeNewadddpjxm" />
    </BaseDialog>
  </el-row>
</template>

<script>
import { getpjOrderList, getStorageList, getBrandspjList } from '@/api/repair/repair/activitesManage';
import newAddpjxm from './newAddpjxm/newAddpjxm';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'addPjxm',
  components: {
    newAddpjxm
  },
  mixins: [dictionary],
  data() {
    return {
      columns: this.createTableColumns(),
      formList: this.createFormList(),
      visibleCxz: false,
      fetchParams: { xhrAbort: true },
      fetchApi: getpjOrderList,
      onChangeColumn: []
    };
  },
  mounted() {
    this.getStorageList();
    this.getBrandspjList();
  },
  methods: {
    validateFn(rule, value, callback) {
      if (value.length < rule.limit) {
        return callback(new Error(rule.message));
      }
      callback();
    },
    // 关闭备件车系弹框
    closeNewadddpjxm(val) {
      if (val != false) {
        this.formList.find(x => x.fieldName === 'partModelGroupCodeSet').initialValue = val[0].PART_MODEL_GROUP_NAME;
      }
      this.visibleCxz = false;
    },
    // 打开备件车系弹框
    cxzClickHandle() {
      this.visibleCxz = true;
    },
    // 品牌下拉框
    async getBrandspjList() {
      const res = await getBrandspjList();
      this.formList.find(x => x.fieldName === 'brand').itemList = res.data.map(x => ({ value: x.BRAND_CODE, text: x.BRAND_NAME }));
    },
    // 仓库名称下拉框
    async getStorageList() {
      const res = await getStorageList();
      this.formList.find(x => x.fieldName === 'STORAGE_CODE').itemList = res.data.map(x => ({ value: x.STORAGE_CODE, text: x.STORAGE_NAME }));
    },
    determine() {
      if (this.onChangeColumn.length != 1) {
        this.$notify.error({
          title: '错误',
          message: '请选择一条内容'
        });
      } else {
        this.$emit('closePj', this.onChangeColumn);
      }
    },
    closeDialog() {
      this.$emit('closePj', false);
    },
    query() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    reset() {
      this.$refs.formPanel.RESET_FORM();
      this.changeHandle();
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '仓库名称',
          fieldName: 'STORAGE_CODE',
          itemList: [],
          placeholder: '请选择',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brand',
          itemList: [],
          placeholder: '请选择',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '备件代码',
          fieldName: 'partCode',
          placeholder: '',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '备件名称',
          fieldName: 'partName',
          placeholder: '',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '拼音代码',
          fieldName: 'spellCode',
          placeholder: '',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '库位代码',
          fieldName: 'STORAGE_POSITION_CODE',
          placeholder: '',
          initialValue: ''
        },
        // {
        //   type: 'INPUT',
        //   label: '车系',
        //   fieldName: 'partModelGroupCodeSet',
        //   placeholder: '',
        //   initialValue: '',
        //   unitRender: () => {
        //     return <el-button icon="el-icon-search" onClick={this.cxzClickHandle}></el-button>;
        //   }
        // },
        {
          type: 'SELECT',
          label: '备件类别',
          fieldName: 'PART_GROUP_CODE',
          placeholder: '请选择',
          itemList: this.createDictList('8075'),
          initialValue: ''
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '仓库名称',
          dataIndex: 'STORAGE_NAME',
          sorter: true
        },
        {
          title: '库位代码',
          dataIndex: 'STORAGE_POSITION_CODE',
          sorter: true
        },
        {
          title: '车系',
          dataIndex: 'PART_MODEL_GROUP_CODE_SET',
          sorter: true
        },
        {
          title: '备件代码',
          dataIndex: 'PART_NO',
          sorter: true
        },
        {
          title: '备件名称',
          dataIndex: 'PART_NAME',
          sorter: true
        },
        {
          title: '拼音代码',
          dataIndex: 'SPELL_CODE',
          sorter: true
        },
        {
          title: '备件类别',
          dataIndex: 'PART_GROUP_CODE',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.PART_GROUP_CODE, '8075')}</span>;
          }
        },
        {
          title: '计量单位代码',
          dataIndex: 'UNIT_CODE',
          sorter: true
        },
        {
          title: '库存数量',
          dataIndex: 'STOCK_QUANTITY',
          sorter: true
        },
        // {
        //     title: '发布时间',
        //     dataIndex: 'RELEASE_DATE',
        //     sorter: true,
        //     dateFormat: 'yyyy-MM-dd'
        // },
        {
          title: '销售价',
          dataIndex: 'SALES_PRICE',
          precision: 2,
          sorter: true
        },
        {
          title: '索赔价',
          dataIndex: 'CLAIM_PRICE',
          precision: 2,
          sorter: true
        },
        {
          title: '销售限价',
          dataIndex: 'LIMIT_PRICE',
          precision: 2,
          sorter: true
        },
        {
          title: '最新进货价',
          dataIndex: 'LATEST_PRICE',
          precision: 2,
          sorter: true
        },
        {
          title: '成本单价',
          dataIndex: 'COST_PRICE',
          precision: 2,
          sorter: true
        },
        {
          title: '成本金额',
          dataIndex: 'COST_AMOUNT',
          precision: 2,
          sorter: true
        },
        {
          title: '最大库存',
          dataIndex: 'MAX_STOCK',
          sorter: true
        },
        {
          title: '最小库存',
          dataIndex: 'MIN_STOCK',
          sorter: true
        },
        {
          title: '借进数量',
          dataIndex: 'BORROW_QUANTITY',
          sorter: true
        },
        {
          title: '借出数量',
          dataIndex: 'LEND_QUANTITY',
          sorter: true
        },
        {
          title: '锁定数量',
          dataIndex: 'LOCKED_QUANTITY',
          sorter: true
        },
        {
          title: '最新入库日期',
          dataIndex: 'LAST_STOCK_IN',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '最新出库日期',
          dataIndex: 'LAST_STOCK_OUT',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true
        }
      ];
    }
  }
};
</script>

<style scoped lang="less">
.bg1 {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  padding-left: 0;
  padding-top: 15px;
  padding-bottom: 15px;
}
.bg2 {
  margin-top: 10px;
  margin-bottom: 10px;
}
.btn {
  text-align: center;
}
.btnStyle {
  margin-top: 10px;
}
</style>
